/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.gui.view;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPopupMenu;
import org.jfonia.connect5.basics.BasicValueNode;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.intervals.Interval;
import org.jfonia.view.main.LeadSheet;
import org.jfonia.model.Sequence;
import org.jfonia.model.StaffCollection;
import org.jfonia.model.elements.Beat;
import org.jfonia.model.elements.Clef;
import org.jfonia.model.elements.Tone;
import org.jfonia.pitch.Base40;
import org.jfonia.pitch.MusicalInterval;
import org.jfonia.view.panels.LeadSheetPanel;
import org.jfonia.view.panels.SimpleLeadSheetPanel;
import percussiongenerator.model.Track;

/**
 *
 * @author Jannes Plyson
 */
public class LeadSheetView extends View{

    private SimpleLeadSheetPanel leadSheetPanel;
    private MutableValueNode width,height;
    public LeadSheetView(Track track,MutableValueNode<View> selectedView,MutableValueNode<ArrayList<Integer>> selectedInterval){
        super(track,selectedView,selectedInterval);
        viewOptions = new LeadSheetViewOptions();
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        createLeadSheet();
        System.out.println("uit create leadsheet");
        this.add(Box.createHorizontalGlue());
    }

    private void createLeadSheet(){
        Sequence<Clef> clefSequence = new Sequence<Clef>(Clef.class);
        Clef clef = new Clef();
        clef.setDifference(track.getStaff().getLength());
        clefSequence.add(clef);
        track.getStaff().addSequence(clefSequence);
        Sequence<Beat> beatSequence = new Sequence<Beat>(Beat.class);
        track.getStaff().addSequence(beatSequence);
        Sequence<Tone> seq = track.getStaff().getToneSequence();
        for(int i = 0; i < seq.size(); i++){
            ((Tone)seq.getIntervalFromIndex(i)).setBase40Rank(Base40.toBase40(new MusicalInterval(6,0)));
        }
        MutableValueNode<Double> xNode = new BasicValueNode<Double>(0.0);
        MutableValueNode<Double> yNode = new BasicValueNode<Double>(0.0);
        width = new BasicValueNode<Double>(700.0);
        height = new BasicValueNode<Double>(700.0);
        LeadSheet leadSheet = new LeadSheet();
        leadSheetPanel = new SimpleLeadSheetPanel(xNode, yNode, width, height);
        this.add(leadSheetPanel);
        leadSheetPanel.setLeadSheet(leadSheet);
        System.out.println("leadsheet added");
        StaffCollection staffCollection = new StaffCollection();
        System.out.println("staffCollection created");
        leadSheet.addStaffCollection(staffCollection);
        System.out.println("staffCollection added");
        System.out.println("adding staff");
        staffCollection.addStaff(track.getStaff());
        System.out.println("adding staff done");
        //width.setValue(700.0);
        //height.setValue(200.0);
    }

    @Override
    public void paintView(Graphics g) {
        leadSheetPanel.repaint();
    }

    public void updateLanguage(ResourceBundle labels) {
        
    }

    public void addElements(JPopupMenu popup, Object caller) {
        
    }

    public void intervalAdded(Interval interval) {

    }

    public void intervalRemoved(Interval interval, int index) {

    }

    public void intervalEndChanged(Interval interval) {
        this.repaint();
    }

    @Override
    public void zoomUpdated() {}

}
